package org.hzero.order.api.controller.v1;

import org.hzero.core.util.Results;
import org.hzero.core.base.BaseController;
import org.hzero.order.domain.entity.Notice;
import org.hzero.order.domain.repository.NoticeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.hzero.mybatis.helper.SecurityTokenHelper;

import io.choerodon.core.domain.Page;
import io.choerodon.core.iam.ResourceLevel;
import io.choerodon.mybatis.pagehelper.annotation.SortDefault;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import io.choerodon.mybatis.pagehelper.domain.Sort;
import io.choerodon.swagger.annotation.Permission;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 通知公告表 管理 API
 *
 * @author hyc 2021-03-11 14:13:44
 */
@RestController("noticeSiteController.v1")
@RequestMapping("/v1/notices")
public class NoticeController extends BaseController {

    @Autowired
    private NoticeRepository noticeRepository;

    @ApiOperation(value = "通知公告表列表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping
    public ResponseEntity<Page<Notice>> list(Notice notice, @ApiIgnore @SortDefault(value = Notice.FIELD_NOTICE_ID,
            direction = Sort.Direction.DESC) PageRequest pageRequest) {
        Page<Notice> list = noticeRepository.pageAndSort(pageRequest, notice);
        return Results.success(list);
    }

    @ApiOperation(value = "通知公告表明细")
    //@Permission(level = ResourceLevel.SITE)
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping("/{noticeId}")
    public ResponseEntity<Notice> detail(@PathVariable Long noticeId) {
        Notice notice = noticeRepository.selectByPrimaryKey(noticeId);
        return Results.success(notice);
    }

    @ApiOperation(value = "创建通知公告表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @PostMapping
    public ResponseEntity<Notice> create(@RequestBody Notice notice) {
        validObject(notice);
        noticeRepository.insertSelective(notice);
        return Results.success(notice);
    }

    @ApiOperation(value = "修改通知公告表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @PutMapping
    public ResponseEntity<Notice> update(@RequestBody Notice notice) {
        SecurityTokenHelper.validToken(notice);
        noticeRepository.updateByPrimaryKeySelective(notice);
        return Results.success(notice);
    }

    @ApiOperation(value = "删除通知公告表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @DeleteMapping
    public ResponseEntity<?> remove(@RequestBody Notice notice) {
        SecurityTokenHelper.validToken(notice);
        noticeRepository.deleteByPrimaryKey(notice);
        return Results.success();
    }

}
